// app.js
const express = require('express');
const cors = require('cors');
const dotenv = require('dotenv');
const path = require('path');

// 加载环境变量
dotenv.config();

const app = express();
const PORT = process.env.PORT || 3000;

// 中间件
app.use(cors());
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use('/uploads', express.static(path.join(__dirname, 'uploads')));

// 路由
const dishRoutes = require('./routes/dishRoutes');
const comboRoutes = require('./routes/comboRoutes');
const orderRoutes = require('./routes/orderRoutes');
const paymentRoutes = require('./routes/paymentRoutes');
const userRoutes = require('./routes/userRoutes');
const categoryRoutes = require('./routes/categoryRoutes'); // 新增
const tableRoutes = require('./routes/tableRoutes'); // 新增
const bannerRoutes = require('./routes/bannerRoutes'); // 新增

app.use('/api/dishes', dishRoutes);
app.use('/api/combos', comboRoutes);
app.use('/api/orders', orderRoutes);
app.use('/api/payments', paymentRoutes);
app.use('/api/users', userRoutes);
app.use('/api/categories', categoryRoutes); // 新增
app.use('/api/tables', tableRoutes); // 新增
app.use('/api/banners', bannerRoutes); // 新增


// 启动服务器 - 只在直接运行此文件时启动
if (require.main === module) {
  app.listen(PORT, () => {
    console.log(`服务器运行在端口 ${PORT}`);
  });
}

// app.js 末尾添加
module.exports = app;